﻿using DiverseStudents.Scripts;
using DiverseStudents.UI;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace DiverseStudents.Mods
{
    internal class ModsUIListView : MonoBehaviour, IMosUICellBase
    {

        public int showCount = 12;
        RectTransform listRect;
        HorizontalLayoutGroup listTitle;
        RectTransform listGroupRect;
        RectTransform listGroupContent;
       
        LinkedList<ModArchive> modArchives;
        private void Create()
        {
            try
            {
                modArchives = new LinkedList<ModArchive> ();
                listRect = UICreate.BeseRectBack("List");
                ScrollRect scrollRect = UICreate.BeseScrollRect();
                scrollRect.transform.SetParent(listRect, false);
                listGroupRect = scrollRect.GetComponent<RectTransform>();
                listGroupContent = scrollRect.content.GetComponent<RectTransform>();
                scrollRect.gameObject.AddComponent<RecyclingListScript>();
            }
            catch (Exception e)
            {
                DLog.Error(e);
            }
        }

        public void Destroy()
        {
            if (listRect != null)
            {
                GameObject.Destroy(listRect.gameObject);
            }
        }

        public void AddItem()
        {

        }


        void ReadModArchives()
        {

        }

      

        public void UpdateRect(RectTransform baseRect)
        {
            if (baseRect == null)
            {
                return;
            }
            Create();
            if (listRect != null)
            {

                Vector2 center = new Vector2(0.5f, 0.5f);
                listRect.anchorMax = center;
                listRect.anchorMin = center;
                listRect.sizeDelta = new Vector2(baseRect.rect.width * 0.7f, baseRect.rect.height * 0.84f);
                listRect.anchoredPosition += new Vector2(-baseRect.rect.width * 0.135f, -baseRect.rect.height * 0.02f);
               
                listRect.SetParent(baseRect, false);

                listTitle = UICreate.HorizontalTile(baseRect.rect.width * 0.7f, baseRect.rect.height * 0.05f);
                listTitle.transform.SetParent(listRect, false);

                var viewPortHeight = baseRect.rect.height * 0.84f - baseRect.rect.height * 0.06f;
                listGroupRect.sizeDelta = new Vector2(baseRect.rect.width * 0.68f, viewPortHeight);
                listGroupRect.anchoredPosition += new Vector2(0, -baseRect.rect.height * 0.028f);
                if (listGroupContent != null)
                {
                    listGroupContent.sizeDelta = new Vector2(baseRect.rect.width * 0.68f, viewPortHeight);
                    listGroupContent.gameObject.AddComponent<ListEmpetDragScript>().UpdateSize(baseRect.rect.width * 0.68f, viewPortHeight / showCount);
                }

            }
        }

      

        #region 循环滑动事件
        #endregion
    }
}
